 <template>
  <div class="app-container">
  <div class="user">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="用户名：" prop="username" required>
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password" v-if="showPassword">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名：" prop="realname"  required>
        <el-input v-model="form.realname"></el-input>
      </el-form-item>
       <el-form-item label="公司名称：" prop="company" required>
        <el-input v-model="form.company"></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="sex" required>
        <el-select v-model="form.sex" placeholder="性别" clearable>
            <el-option
                v-for="item in this.sexList"
                :key="item.id"
                :label="item.sex"
                :value="item.id"
              ></el-option>
       
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期：" prop="addTime" required>
        <el-date-picker v-model="form.addTime"  type="date"  />
      </el-form-item>
      <el-form-item label="手机号：" prop="phone" required>
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <div class="userpass">
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm" style="margin-left:80px">重置</el-button>
      </el-form-item>
      </div>
    </el-form>
    
  </div>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
import { check,edit,getdetail,getpage} from "@/api/user";
import Editor from '@/components/Editor';
export default {
  inject:['reload'],
   components: {
    Editor
  },
  data() {
    return {
      form: {
         id: 0,
        username: "",
        password: "",
        realname: "",
        company:"",
        addTime:"",
        sex: "",
        phone: null
      },
      sexList:[
          {sex: '男', id: 1},
          {sex: '女', id: 2}
      ],
      roleList:[],
      showPassword : true,
      formLoading: false
    };
  },
  created() {
    //console.log(this.sexList)
    let id = this.$route.query.id;
    let _this = this;
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true;
      console.log(id)
      getdetail({ id: id}).then(re => {
        _this.form = re.data; 
        _this.formLoading = false;
      });
    }
  },
  methods: {
      submitForm() {
      let _this = this;
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          edit(this.form)
            .then(data => {
              if (data.errno === 0) {
                _this.$message.success('操作' + data.message);          
                _this.$router.push("/user/all/index");
              } else {
                _this.$message.error('操作' + data.message);
                _this.formLoading = false;
              }
            })
            .catch(e => {
              _this.formLoading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm () {
      this.reload()
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.user {
  margin-top: 20px;
  margin-left:80px;
}
.userpass{
    margin: 50px;
}
</style>
